<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加员工</title>
<link rel="stylesheet" href="../libs/layui/css/layui.css">
<script type="text/javascript" src="../libs/layui/layui.js"></script>
</head>
<body>

<form class="layui-form"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
  <div class="layui-form-item layui-inline">
    <label class="layui-form-label">姓名</label>
    <div class="layui-input-block">
      <input type="text" name="key" placeholder="请输入" autocomplete="off" class="layui-input">
    </div>
  </div>
 <div class="layui-form-item layui-inline">
	    <label class="layui-form-label">性别</label>
	    <div class="layui-input-block">
	      <select name="sex" lay-verify="required" id="selectbysex">
	      </select>
	    </div>
  </div>
  <div class="layui-form-item layui-inline">
	    <label class="layui-form-label">部门</label>
	    <div class="layui-input-block">
	      <select name="dep" lay-verify="required" id="selectbydep">
	      </select>
	    </div>
  </div>
    <div class="layui-form-item layui-inline">
    <label class="layui-form-label">生日</label>
    <div class="layui-input-block">
      <input type="text" name="bir" placeholder="请输入" autocomplete="off" class="layui-input">
    </div>
  </div>  
  <div class="layui-form-item layui-inline">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
  <!-- 更多表单结构排版请移步文档左侧【页面元素-表单】一项阅览 -->
</form>

</body>
<script>
layui.use(['table','form'], function(){
  var table = layui.table;
  var form = layui.form;
  var $= layui.$;
 
  
  //部门下拉菜单
  $(function(){
	  $.ajax({
			url:"../departmentServlet",
			type:"get",
			data:{}
			,
			success:function(res){
				var $select = $("#selectbydep");
				for(var i=0;i<res.data.length;i++){
					$select.append("<option value='"+res.data[i].dptId+"'>"+res.data[i].dptName+"</option>");
					//遍历部门编号和部门名称，将部门编号赋给value,将部门名称展示给用户
				}
				form.render('select');
			}
		});
	  
  })
  
  
  //性别下拉菜单
  $(function(){
	  $.ajax({
			type:"get",
			data:{}
			,
			success:function(res){
				var $select = $("#selectbysex");
				$select.append("<option value='男'>"+"男"+"</option>");
				$select.append("<option value='女'>"+"女"+"</option>");
				form.render('select');
			}
		});
	  
  })
  
  
  //提交监听事件
  form.on('submit(*)', function(data){
	  console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
	  $.ajax({
		  url:'../addEmployeeServlet',
		  type:'post',
		  data:data.field,
		  success:function(res){
			  if(res.code>0){
				  layer.msg("添加成功");
			  }
			  else{
				  layer.msg("添加失败");
			  }
		  }
	  });
	  return false;  //阻止表单跳转。如果需要表单跳转，去掉这段即可。
	});
  
});
</script>
</html>